package com.example.chatroom.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public CommandLineRunner initDatabase() {
        return args -> {
            try (var connection = dataSource.getConnection()) {
                var statement = connection.createStatement();
                // 删除已存在的表（如果有）
                statement.execute("DROP TABLE IF EXISTS messages");
                // 创建新表，使用CLOB类型存储content
                statement.execute(
                    "CREATE TABLE messages (" +
                    "id BIGINT AUTO_INCREMENT PRIMARY KEY," +
                    "sender VARCHAR(255)," +
                    "receiver VARCHAR(255)," +
                    "content CLOB," +
                    "type VARCHAR(50)," +
                    "file_name VARCHAR(255)," +
                    "timestamp TIMESTAMP" +
                    ")"
                );
            }
        };
    }
} 